package com.itheima.pattern.t1_singleton.demo8;

import java.lang.reflect.Constructor;

/**
 * 测试使用反射破坏单例模式
 */
public class TestDemo {

    public static void main(String[] args) throws Exception {
        // 1、获取Singleton的字节码对象
        Class clazz = Singleton.class;
        // 2、获取无参构造方法对象
        Constructor constructor = clazz.getDeclaredConstructor();
        // 3、取消访问检查
        constructor.setAccessible(true);
        // 4、创建对象
        Singleton instance1 = (Singleton) constructor.newInstance();
        Singleton instance2 = (Singleton) constructor.newInstance();

        System.out.println(instance1 == instance2);
    }
}
